﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warehouse.Entites;
using Warehouse.Entites.WMS;
using WCS.Entities;
using WCS.Entities.BulkStorage;
using WCS.Entities.TES;
using WCS.Entities.User;


namespace Warehouse.Services.Repository
{
    public static class SqlsugarSetup
    {
        private readonly static List<string> isNullFileds = new List<string>()
        {

        };

        private readonly static List<string> isJsonFileds = new List<string>()
        {

        };
        private readonly static List<string> isIgnoreFields = new List<string>()
        {
            "Operation"
        };

        public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, string dbName = "db_master")
        {
            SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                // ConfigId = "yingfa",
                ConnectionString = configuration[dbName],
                DbType = SqlSugar.DbType.MySql,


                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    EntityService = (type, column) =>
                    {
                        //忽略的字段
                        if (isIgnoreFields.Contains(type.Name))
                        {
                            column.IsIgnore = true;
                        }
                        //可空字段。
                        if (isNullFileds.Contains(type.Name))
                        {
                            column.IsNullable = true;
                        }
                        //转为json字符串。
                        if (isJsonFileds.Contains(type.Name))
                        {
                            column.IsJson = true;
                        }
                    }
                }
            },
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        // Console.WriteLine(sql);//输出sql
                    };
                });


            // sqlSugar.DbMaintenance.CreateDatabase();
            sqlSugar.CodeFirst.SetStringDefaultLength(400).InitTables(
                    typeof(Material),
                    typeof(OutboundShipments),
                    typeof(Storage),
                    typeof(WareHouse),
                    typeof(UserInfo),
                    typeof(Job),
                    typeof(Device),
                    typeof(TransferPort),
                    typeof(Siterelease),
                    typeof(Redatport),
                    typeof(Binding),
                    typeof(BulkStorage),
                    typeof(SingleBox),
                    typeof(BOX),
                    typeof(Types),
                    typeof(MaterialClass),
                    typeof(FeederSites),
                    typeof(Ianeway),
                    typeof(Salver),
                    typeof(DispatchHandling));
            sqlSugar.Ado.CommandTimeOut = 300;
            sqlSugar.QueryFilter.AddTableFilter<Job>(it => it.Isdeleted == false);
            services.AddSingleton<ISqlSugarClient>(sqlSugar);
        }
    }
}
